#include "HttpServer.hpp"
#include "Daemon.hpp"
#include "Log.hpp"

using namespace LogModule;

void Login(HttpRequest& req, HttpResponse& resp)
{
    //根据req,创建resp
    LOG(LogLevel::DEBUG) << "进入到登录页面" << req.Path() << ", " << req.Args();
    //req.Path(): /login
    //req.Args(): username=admin&password=123456
    std::string req_args = req.Args();
    //1.解析参数格式，得到想要的参数

    //2.验证用户名和密码是否正确
    // SessionManager m;
    // uint64_t session_id = m.CreateSession(XXXXXXX);

    //3.登录成功
    std::string body =  req.GetContent("wwwroot/success.html");
    resp.SetCode(200);
    resp.SetHeader("Content-Length", std::to_string(body.size()));
    resp.SetHeader("Content-Type", "text/html");
    resp.SetHeader("Set-Cookie", "username=zhangsan&passwd=123456");
    resp.SetBody(body);

    //4.登录成功后，重定向到首页或其他页面
    // resp.SetCode(302);
    // resp.SetHeader("Location", "/"); 
}



void Register_(HttpRequest& req, HttpResponse& resp)
{
    LOG(LogLevel::DEBUG) << "进入到注册页面" << req.Path() << ", " << req.Args();
}

void Search(HttpRequest& req, HttpResponse& resp)
{
    LOG(LogLevel::DEBUG) << "进入到搜索页面" << req.Path() << ", " << req.Args();
}


int main(int argc, char* argv[])
{
    if(argc!= 2)
    {
        std::cout << "Usage: " << argv[0] << " port" << std::endl;
        return 1;
    }
    std::unique_ptr<HttpServer> httpserver = std::make_unique<HttpServer>(std::stoi(argv[1]));
    //让服务器具有注册功能
    //restful风格的请求接口
    httpserver->Register("/login", Login);
    httpserver->Register("/register", Register_);
    httpserver->Register("/search", Search);
    httpserver->Start();
    return 0;
}